<template>
	<!-- 4.5地址列表 -->
	<view>
		<u-navbar title="我的地址" :autoBack="true" :placeholder="true"></u-navbar>

		<view class="box" v-for="item in ressList" :key="item.id">
			<template v-if="typeFlag =='false'">
				<image v-if="item.id == orderId" class="check" src="http://qiniu.suboshimuying.cn/static/cart/check.png"
					mode="">
				</image>
				<image v-else class="check" src="http://qiniu.suboshimuying.cn/static/order/icon24@2x.png" mode=""
					@click="itemClick(item.id)">
				</image>
			</template>
			<view class="main">
				<view class="nameBox">
					<view class="left">
						<!-- <image src="../../static/images/my/dizhi.png" mode=""></image> -->
						<view class="name">{{item.name}}</view>
						<view class="phone">{{item.mobile}}</view>
						<view class="defult-address" v-if="item.isDefault == 1">默认地址</view>
						<view class="set-defult" v-else @click="setDefault(item.id)">设为默认</view>
					</view>
					<view class="right" v-if="typeFlag != 'false'">

						<view class="iconfont icon-shanchu" @click="delClick(item.id)"></view>
						<view class="iconfont icon-bianji"
							@click="toUrl(`/subPages/newAddress/newAddress?type=${2}&id=${item.id}`)"></view>
					</view>
				</view>
				<view class="address">
					<view class="iconfont icon-weibiaoti-3"></view>
					{{item.area}}&nbsp;&nbsp;{{item.address}}
				</view>
			</view>
		</view>
		<view class="btnBox">
			<view class="btn" @click="toUrl(`/subPages/newAddress/newAddress?type=${1}`)">新增收货地址</view>
			<view class="btn" @click="btnClick" v-if="typeFlag=='false'">确定</view>
		</view>
		<MessageBox @popupClose="showFlag=false" :showFlag="showFlag" @MesBoxCallback="callback" />
	</view>
</template>

<script>
	import MessageBox from '@/components/MessageBox/MessageBox.vue'
	export default {
		data() {
			return {
				typeFlag: true,
				ressList: [], //地址列表
				orderId: null, //订单选择的地址id
				showFlag: false, //弹出层
				delId: null, //删除ID
			};
		},
		components: {
			MessageBox
		},
		onLoad(option) {
			// type: true查看地址列表 false: 选择地址
			console.log(option);
			this.typeFlag = option.type
			console.log(this.typeFlag);
			if (option.type == "false") {
				this.orderId = option.id
			} else {
				this.orderId = null
			}
		},
		onShow() {
			this.getRessList()
		},
		methods: {
			// 跳转
			toUrl(url) {
				uni.navigateTo({
					url
				})
			},
			// 地址点击
			itemClick(id) {
				if (this.typeFlag == "false") {
					this.orderId = id
				}
			},
			// 按钮点击
			btnClick() {
				// 选择地址操作
				const eventChannel = this.getOpenerEventChannel();
				let arr = this.ressList.filter(item => item.id === this.orderId)
				if (arr.length == 0) {
					this.$u.toast('请选择地址')
				} else {
					eventChannel.emit('orderAreaBack', arr[0]);
					uni.navigateBack({});
				}

			},
			// 删除按钮点击
			delClick(id) {
				this.delId = id
				this.showFlag = true
			},
			// 弹出层回调
			callback() {
				this.del()
			},
			// 删除
			async del() {
				let res = await this.$api.delress({
					id: this.delId,
				})
				this.$u.toast('删除成功')
				this.showFlag = false
				if (res.code === 1) {
					setTimeout(() => {
						this.getRessList()
					}, 1500)
				}
			},
			// 获取地址列表
			async getRessList() {
				let res = await this.$api.getRessList({
					uid: wx.getStorageSync('userInfo').uid
				})
				this.ressList = res.data.sort(this.compare('isDefault')).reverse();
			},
			// 设为默认地址
			async setDefault(id) {
				let res = await this.$api.setDefault({
					id: id,
					uid: wx.getStorageSync('userInfo').uid
				})
				if (res.code === 0) {
					this.getRessList();
				}
			},

			compare(property) {
				return function(obj1, obj2) {
					var value1 = obj1[property];
					var value2 = obj2[property];
					return value1 - value2; // 升序
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.box {
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		padding: 32rpx;
		margin: 20rpx 0;
		background-color: #fff;

		.main {
			flex: 1;
		}

		.nameBox {
			display: flex;
			justify-content: space-between;
			margin-bottom: 20rpx;

			.left {
				display: flex;
				align-items: center;



				.name {
					margin: 0 20rpx 0 0;
					color: rgba(0, 0, 0, 0.8);
					font-size: 32rpx;
					font-weight: 700;
				}

				.phone {
					margin-top: 4rpx;
					color: rgba(0, 0, 0, 0.8);
					font-size: 28rpx;
					font-weight: 700;
				}

				.defult-address {
					font-size: 24rpx;
					font-weight: 700;
					color: white;
					height: 34rpx;
					line-height: 34rpx;
					background-color: #1c681e;
					border-radius: 5rpx;
					margin: 0 10rpx;
					padding: 0 10rpx;
				}

				.set-defult {
					font-size: 24rpx;
					font-weight: 700;
					color: white;
					height: 34rpx;
					line-height: 34rpx;
					background-color: #a6a0a4;
					border-radius: 5rpx;
					margin: 0 10rpx;
					padding: 0 10rpx;
				}
			}

			.right {
				display: flex;
				align-items: center;



				.iconfont {
					margin-left: 32rpx;
					width: 36rpx;
					height: 36rpx;
				}
			}
		}

		.address {
			color: rgba(0, 0, 0, 0.4);
			font-size: 26rpx;
			display: flex;

			.iconfont {
				font-size: 28rpx;
				font-weight: 700;
				margin-right: 10rpx;
			}
		}

		.line {
			position: absolute;
			width: 100%;
			height: 12rpx;
			margin-top: 32rpx;
			background: rgba(248, 249, 254, 1);
		}

		.check {
			margin-right: 22rpx;
			width: 36rpx;
			height: 36rpx;
		}
	}


	.btnBox {
		width: 100%;
		box-sizing: border-box;
		position: fixed;
		left: 0;
		bottom: 84rpx;
		padding: 0 32rpx;
		display: flex;
		justify-content: space-around;
		align-items: center;

		.btn {
			// display: flex;
			// justify-content: center;
			// align-items: center;
			// width: 686rpx;
			// flex: 1;
			width: calc(100vw - 40rpx);
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			// box-sizing: border-box;
			// padding: 25rpx 50rpx;
			// margin: 0 15rpx;
			// height: 88rpx;
			border-radius: 600rpx;
			color: rgba(255, 255, 255, 1);
			font-size: 32rpx;
			font-weight: 500;
			background: #111;
		}
	}
</style>